<template>
  <el-card>
    <el-table border :data="skuList" style="margin: 10px 0 20px 0;">
      <el-table-column label="序号" type="index" width="80px" align="center"></el-table-column>
      <el-table-column label="名称" show-overflow-tooltip align="center" prop="skuName"></el-table-column>
      <el-table-column label="描述" show-overflow-tooltip align="center" prop="skuDesc"></el-table-column>
      <el-table-column label="默认图片" align="center">
        <template #="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px;">
        </template>
      </el-table-column>
      <el-table-column label="重量(g)" align="center" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" align="center" prop="price"></el-table-column>
      <el-table-column label="操作" width="250px" fixed="right">
        <template #="{ row }">
          <el-button :type="row.isSale ? 'info' : 'success'" size="small"
                     :icon="row.isSale ? 'Bottom' : 'Top'"
                     @click="changeSaleStatus(row)"></el-button>
          <el-button type="primary" size="small" icon="Edit" @click="ElMessage.warning('功能开发中。。。')"></el-button>
          <el-button type="info" size="small" icon="infoFilled" @click="checkSkuInfo(row)"></el-button>
          <el-popconfirm :title="`确定删除${row.skuName}?`" @confirm="deleteSku(row)">
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
                   v-model:current-page="currentPage"
                   v-model:page-size="pageSize"
                   :page-sizes="[10, 20, 30, 40]"
                   :background="true"
                   layout="prev, pager, next, jumper, ->, sizes, total"
                   :total="total"
                   @current-change="getSkuList"
                   @size-change="pageSizeChange" />
    <el-drawer v-model="isShowDrawer" title="查看商品详情">
      <el-row style="margin: 10px 0;">
        <el-col :span="6">名称</el-col>
        <el-col :span="18">{{ skuInfo?.skuName }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0;">
        <el-col :span="6">描述</el-col>
        <el-col :span="18">{{ skuInfo?.skuDesc }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0;">
        <el-col :span="6">价格</el-col>
        <el-col :span="18">{{ skuInfo?.price }}</el-col>
      </el-row>
      <el-row style="margin: 10px 0;">
        <el-col :span="6">平台属性</el-col>
        <el-col :span="18">
          <el-tag v-for="item in skuInfo?.skuAttrValueList" style="margin: 5px;">{{ item.attrName }}</el-tag>
        </el-col>
      </el-row>
      <el-row style="margin: 10px 0;">
        <el-col :span="6">销售属性</el-col>
        <el-col :span="18">
          <el-tag v-for="item in skuInfo?.skuSaleAttrValueList" style="margin: 5px;">{{ item.saleAttrName }}</el-tag>
        </el-col>
      </el-row>
      <el-row style="margin: 10px 0;">
        <el-col :span="6">商品图片</el-col>
        <el-col :span="18">
          <el-carousel :interval="4000" type="card" height="200px">
            <el-carousel-item v-for="item in skuInfo?.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width: 100%;">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>

    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { reqCancelSale, reqDeleteSku, reqOnSale, reqSkuInfo, reqSkuList } from '@/api/product/sku';
import type { SkuResponseData, SkuData } from '@/api/product/sku/type';
import { ElCol, ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';

const currentPage = ref<number>(1)
const pageSize = ref<number>(10)
const total = ref<number>(0)

const skuList = ref<SkuData[]>([])
const skuInfo = ref<SkuData>()

const isShowDrawer = ref<boolean>(false)

onMounted(() => {
  getSkuList(currentPage.value)
})

const getSkuList = async (pager: number = 1) => {
  currentPage.value = pager
  const result: SkuResponseData = await reqSkuList(currentPage.value, pageSize.value)
  skuList.value = result.data.records
  total.value = result.data.total
}

const pageSizeChange = () => {
  getSkuList()
}

// 改变SKU销售状态
const changeSaleStatus = async (row: SkuData) => {
  if (row.isSale) {
    const result = await reqCancelSale(row.id as number)
    if (result.code === 200) {
      ElMessage.success('下架成功')
    } else {
      ElMessage.error('下架失败')
    }
  } else {
    const result = await reqOnSale(row.id as number)
    if (result.code === 200) {
      ElMessage.success('上架成功')
    } else {
      ElMessage.error('上架失败')
    }
  }
  getSkuList(currentPage.value)
}

// 查看SKU详情
const checkSkuInfo = async (row: SkuData) => {
  isShowDrawer.value = true
  const result = await reqSkuInfo(row.id as number)
  if (result.code === 200) {
    skuInfo.value = result.data
  }
}

// 删除SKU
const deleteSku = async (row: SkuData) => {
  const result = await reqDeleteSku(row.id as number)
  if (result.code === 200) {
    ElMessage.success('删除成功')
    getSkuList(skuList.value.length > 1 ? currentPage.value : currentPage.value - 1)
  } else {
    ElMessage.error(result.data)
  }
}
</script>

<style scoped></style>
